import java.awt.*;
import java.util.ArrayList;

//Written for java 1.5

public class AppLauncherApplet extends java.applet.Applet 
{

	public static final long serialVersionUID = 1;
	
	//Widgets
	ArrayList<String> applications = new ArrayList<String>();
	ArrayList<Button> buttons = new ArrayList<Button>();


    public void init () 
    {

		//Getting parameters
		int numberOfApplications = Integer.parseInt(this.getParameter("numberOfApplications"));

		//Create buttons for the applications
		for (int i=0;i<numberOfApplications;i++)
		{
			String display = this.getParameter("display"+i);
			String application = this.getParameter("location"+i);
			applications.add(application);
			buttons.add(new Button(display));
		}
	
	    // Initialize the window.
    	setBackground(Color.LIGHT_GRAY); 
    	setForeground(Color.BLUE); 
    	setLayout(new FlowLayout());

	    //draw out the buttons for each application
		for (int i=0;i<buttons.size();i++)
		{
	    	add ((Button)buttons.get(i)); 	
		}
    }
    
    public boolean action (Event evt, Object arg) 
    {

    	if (evt.target instanceof Button) 
    	{
        	Button clickedButton = (Button)evt.target;

        	//Look up the application to run
          	int applicationIndex = buttons.indexOf(clickedButton);
          	if (applicationIndex>-1)
          	{
				String application = (String)applications.get(applicationIndex);
				try 
				{
		            Runtime.getRuntime().exec(application);
		            //Process p = Runtime.getRuntime().exec(application);
        		}
        		catch (Exception e) 
        		{
            		System.out.println("\nProblem trying to launch application:"+application+".\n");;
		            e.printStackTrace();
        		}
          	}
        }
        return true;
    } 
}
